package com.jeremyfeinstein.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class DateFormatUtil {
    /**
     * 将Date转为 yyyy-MM-dd HH:mm:ss 格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String dateToLongString(Date date) {
        return String.format(Locale.getDefault(), "%tF %tT", date, date);
    }

    /**
     * 将日期转为yyyy-MM-dd格式的字符串
     *
     * @param date
     * @return
     */
    public static String dateToShortString(Date date) {
        return String.format("%tF", date);
    }

    /**
     * 将日期转为 HH:mm格式的字符串
     *
     * @param date
     * @return
     */
    public static String dateToShortTime(Date date) {
        return String.format("%tR", date);
    }

    /**
     * 将日期格式转为yyyy-MM-dd HH:mm 格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String dateToShortDateTime(Date date) {
        return String.format("%tF %tR", date, date);
    }

    /**
     * 将字符串转换为Date类型
     *
     * @param dateString
     * @return
     */
    public static Date parseDateFromString(String dateString) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            return sdf.parse(dateString);
        } catch (Exception e) {

        }
        return new Date();
    }

    /**
     * 判断两天是否为同一天
     *
     * @param preDate
     * @param currDate
     * @return
     */
    public static boolean isSameDay(Date preDate, Date currDate) {
        if (dateToShortString(preDate).equals(dateToShortString(currDate))) {
            return true;
        }
        return false;
    }

    public final static int compareDate(Date beg, Date end) {
        int res = 0;
        if (beg != null && end != null) {
            res = (int) (end.getTime() - beg.getTime()) / 1000;
        }
        return res;
    }

    public static String millisToLongDate(long millis) {
        Date date = new Date(millis);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        String sb = format.format(gc.getTime());
        return sb;
    }

    public static String millisToShortDate(long millis) {
        Date date = new Date(millis);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        String sb = format.format(gc.getTime());
        return sb;
    }

    /**
     * 用于将毫秒数转换为00:00 或 00:00:00格式
     *
     * @param time
     * @return
     */
    public static String formateTime(long time, String type) {
        time /= 1000;
        long minute = time / 60;
        long hour = minute / 60;
        long second = time % 60;
        minute %= 60;
        String result = null;
        if (type == null) {
            type = "00:00";
        }
        if (type.equals("00:00")) {
            result = String.format("%02d:%02d", minute, second);
        }
        if (type.equals("00:00:00")) {
            result = String.format("%02d:%02d:%02d", hour, minute, second);
        }
        return result;
    }

    public static String formateTimeToSoconds(long time) {
        time /= 1000;
        long minute = time / 60;
        long hour = minute / 60;
        long second = time % 60;
        minute %= 60;
        String result = String.valueOf(hour * 3600 + minute * 60 + second);
        return result;
    }

    public static String getCurrentDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.CHINA);
        return formatter.format(new Date());
    }


}
